package com.xms.schema.service.attribute;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xms.datacore.service.FinderService;
import com.xms.schema.core.AttributeTypeIds;
import com.xms.schema.entity.Attribute;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * AttributeFinderService
 * </p>
 *
 * @author migo
 * @since 2021/9/2
 */
@CacheConfig(cacheNames = "schema.attribute", keyGenerator = "CacheKeyGenerator")
@Service
public class AttributeFinderService extends FinderService<Attribute> implements IAttributeFinderService {

    @Cacheable(condition="#id != null")
    @Override
    public Attribute getById(UUID id) {
        return super.getById(id);
    }

    @Cacheable(condition="#idList != null && #idList.size() > 0")
    @Override
    public List<Attribute> getByIds(Collection<UUID> idList) {
        return super.listByIds(idList);
    }

    @Cacheable()
    @Override
    public Attribute getOne(Wrapper<Attribute> queryWrapper) {
        return super.getOne(queryWrapper);
    }

    @Cacheable()
    @Override
    public int count() {
        return super.count();
    }

    //@Cacheable()
    @Override
    public int count(Wrapper<Attribute> queryWrapper) {
        return super.count(queryWrapper);
    }

    //@Cacheable()
    @Override
    public List<Attribute> list(Wrapper<Attribute> queryWrapper) {
        return super.list(queryWrapper);
    }

    @Cacheable()
    @Override
    public List<Attribute> all() {
        return super.list();
    }

    @Cacheable(condition="#entityId != null && #name != null")
    @Override
    public Attribute getByName(UUID entityId, String name) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityid, entityId)
                .eq(Attribute::getName, name);
        return super.getOne(queryWrapper);
    }

    @Cacheable(condition="#entityName != null && #name != null")
    @Override
    public Attribute getByName(String entityName, String name) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityname, entityName)
                .eq(Attribute::getName, name);
        return super.getOne(queryWrapper);
    }

    @Cacheable(condition="#entityId != null && #name != null")
    @Override
    public List<Attribute> getByName(UUID entityId, Collection<String> name) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityid, entityId)
                .in(Attribute::getName, name);
        return super.list(queryWrapper);
    }

    @Cacheable(condition="#entityName != null && #name != null")
    @Override
    public List<Attribute> getByName(String entityName, Collection<String> name) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityname, entityName)
                .in(Attribute::getName, name);
        return super.list(queryWrapper);
    }

    @Cacheable(condition="#entityId != null")
    @Override
    public List<Attribute> getByEntityId(UUID entityId) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityid, entityId);
        return super.list(queryWrapper);
    }

    @Cacheable(condition="#entityName != null")
    @Override
    public List<Attribute> getByEntityName(String entityName) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityname, entityName);
        return super.list(queryWrapper);
    }

    @Cacheable(condition="#entityId != null")
    @Override
    public List<Attribute> getBySummaryEntityId(UUID entityId) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getSummaryentityid, entityId);
        return super.list(queryWrapper);
    }

    @Cacheable(condition="#entityId != null")
    @Override
    public Attribute getPrimaryKey(UUID entityId) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityid, entityId)
                .eq(Attribute::getAttributetypename, AttributeTypeIds.PRIMARYKEY);
        return super.getOne(queryWrapper);
    }

    @Cacheable(condition="#entityName != null")
    @Override
    public Attribute getPrimaryKey(String entityName) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityname, entityName)
                .eq(Attribute::getAttributetypename, AttributeTypeIds.PRIMARYKEY);
        return super.getOne(queryWrapper);
    }

    @Cacheable(condition="#entityId != null")
    @Override
    public Attribute getPrimaryField(UUID entityId) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityid, entityId)
                .eq(Attribute::getIsprimaryfield, true);
        return super.getOne(queryWrapper);
    }

    @Cacheable(condition="#entityName != null")
    @Override
    public Attribute getPrimaryField(String entityName) {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getEntityname, entityName)
                .eq(Attribute::getIsprimaryfield, true);
        return super.getOne(queryWrapper);
    }

    @Cacheable()
    @Override
    public List<Attribute> getAuthorizations() {
        LambdaQueryWrapper<Attribute> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Attribute::getAuthorizationenabled, true);
        return super.list(queryWrapper);
    }
}
